package com.jiukuaidao.zhulin.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jiukuaidao.library.base.BaseHolder;
import com.jiukuaidao.library.base.BaseXrecyclerViewAdapter;
import com.jiukuaidao.library.widget.recyclerview.XRecyclerView;
import com.jiukuaidao.zhulin.R;
import com.jiukuaidao.zhulin.util.AddressInfoUtil;
import com.jiukuaidao.zhulin.module.address.ChangePositionActivity;
import com.jiukuaidao.zhulin.module.address.bean.EventGainAddressBean;
import com.jiukuaidao.zhulin.module.address.bean.PreAddressBean;
import com.ta.utdid2.android.utils.StringUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.List;
import java.util.Map;

/**
 * Author : PaiTaSuo
 * Date : 2016/6/8
 * Description :
 */
public class GainPlaceAdapter extends BaseXrecyclerViewAdapter {
    private List<Map<String, String>> mList;
    private String mType;
    private Activity mActivity;
    private String mAdapterType;

    public GainPlaceAdapter(Context context, int layoutId, List datas, XRecyclerView recyclerView, String type, String adapterType) {
        super(context, layoutId, datas, recyclerView);
        mList = datas;
        mType = type;
        mActivity = (Activity) context;
        mAdapterType = adapterType;
    }

    @Override
    public void updateUI(BaseHolder holder, int position, Object data) {

        TextView name = holder.getView(R.id.item_text);
        ImageView location = holder.getView(R.id.iv_search_location);
        TextView address = holder.getView(R.id.address_item_text);
        if (!StringUtils.isEmpty(mList.get(position).get("address"))) {
            address.setText(mList.get(position).get("address"));
            address.setVisibility(View.VISIBLE);
        } else {
            address.setVisibility(View.GONE);
        }
        if (mType.equals("Geo")) {
            if (position == 0) {
                name.setText("[当前]" + mList.get(position).get("name"));
                name.setTextColor(mContext.getResources().getColor(R.color.text_red_ff404a));
                location.setBackgroundResource(R.mipmap.ic_address_search_position);
                address.setTextColor(mContext.getResources().getColor(R.color.text_black_252525));
            } else {
                name.setText(mList.get(position).get("name"));
                name.setTextColor(mContext.getResources().getColor(R.color.text_black_252525));
                location.setBackgroundResource(R.mipmap.ic_address_position);
                address.setTextColor(mContext.getResources().getColor(R.color.text_black_252525));
            }

        }
        if (mType.equals("Sug")) {
            name.setText(mList.get(position).get("name"));
            name.setTextColor(mContext.getResources().getColor(R.color.text_black_252525));
            location.setBackgroundResource(R.mipmap.ic_address_position);
            address.setTextColor(mContext.getResources().getColor(R.color.text_gray_999999));
        }
    }

    @Override
    protected void onItemClick(BaseHolder holder, int position) {
        super.onItemClick(holder, position);

        if (mAdapterType.equals("gain")) {
            EventGainAddressBean addressBean = new EventGainAddressBean();
            addressBean.address = mList.get(position).get("address");
            addressBean.city = mList.get(position).get("city");
            addressBean.cityCode = mList.get(position).get("cityCode");
            addressBean.name = mList.get(position).get("name");
            addressBean.longitude = mList.get(position).get("longitude");
            addressBean.latitude = mList.get(position).get("latitude");
            EventBus.getDefault().post(addressBean);
            mActivity.finish();
            return;
        }

        if (mAdapterType.equals("search")) {
            Map<String, String> map = mList.get(position);
            //当前位置存入首选项
            AddressInfoUtil.cleanAddressInfo();
            PreAddressBean mAddressInfo = new PreAddressBean();
            mAddressInfo.longitude = map.get("longitude");
            mAddressInfo.latitude = map.get("latitude");
            mAddressInfo.cityCode = map.get("cityCode");
            mAddressInfo.address = map.get("name");
            AddressInfoUtil.saveAddressInfo(mAddressInfo);
            EventBus.getDefault().post(map);
            mActivity.finish();
            ChangePositionActivity.mInstance.finish();
        }
    }
}
